package com.my.screenadapter.fragmenttest;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.my.screenadapter.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * Created by 张英杰 on 2017/1/11.
 */

public class TwoFragment extends Fragment {

    private TextView tv;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        tv = (TextView) view.findViewById(R.id.tv);
        tv.setText("Two-----");
//        step2：注册事件接收者
//        这里MainFragment是要接收MenuFragment发送来的数据，
//        所以我们在MainFragment中的onCreateView()方法中进行注册：
        EventBus.getDefault().register(this);
        return view;
    }

//    step4：接收消息并处理
//     3.0版本必须要写注解@Subscribe
    @Subscribe
    public void abc(String str){
        tv.setText(str);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void setData(String str) {
        tv.setText(str);
    }
}
